﻿using System;
using System.Drawing.Printing;
using System.Threading.Tasks;
using System.Web.Http;
using Top.Web;
using Top.Web.Api;
using Top.PointStore.Business;
using Top.PointStore.Business.Models;
using Top.PointStore.LuckTurntable;
using Top.PointStore.Web.Filters;
using Top.Web.Api.Filters;
using Top.Core;

namespace Top.PointStore.Controllers
{
    [Provider]
    public class LuckBigTurntableController : ApiBase
    {
        /// <summary>
        /// 抽奖
        /// </summary>
        public async Task<object> Turn()
        {
            if (MemoryTable.TB_PointMain_new.ContainsKey(User.UserToken.ToString().ToUpper()))
            {
                T_PointMain_new model = MemoryTable.TB_PointMain_new[User.UserToken.ToString().ToUpper()];
                var res = await LuckBigWheel.Turn_RedPacket(User, model);
                return res;
            }
            else
            {
                var res = await LuckBigWheel.Turn_RedPacket(User, null);
                return res;
            }                    
        }

        [NoLogin]
        public object GetUser()
        {
            return User;
        }

        /// <summary>
        /// 根据经纪人获取中奖纪录
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        [NoLogin]
        public async Task<object> GetLuckRecored(int pageIndex = 1, int pageSize = 10)
        {
            return await LuckTurntableManager_Plus.GetLuckRecordListByBrokerKid(User.UserToken.ToString(), Pager.Create(pageIndex, pageSize));
        }

        [NoLogin]
        public async Task<bool> CheckLuckyInfo(string brokerUserToken, string outTransactionNumber, double amount)
        {
            return await LuckBigWheel.CheckLuckyRecord(brokerUserToken, outTransactionNumber, amount);
        }

        [Login]
        public async Task<object> GetLuckList(string cityKid, string brokerPhone = "", string brokerName = "", string startTime = "", string endTime = "", int isSend = -1, int isWin = -1, string productName = null, int pageIndex = 1, int pageSize = 10)
        {
            int cid = -1;
            if (!string.IsNullOrWhiteSpace(cityKid)&&cityKid!="undefined")
            {
                cid = Convert.ToInt32(cityKid);
            }
            return await LuckTurntableManager_Plus.GetLuckList(cid,pUser.PointStoreAuth.F_CityKid, brokerPhone, brokerName, startTime, endTime, isSend, isWin, productName, Pager.Create(pageIndex, pageSize));
        }

        public async Task<object> UpdateSendStatus(int id, int isSend)
        {
            return await LuckTurntableManager_Plus.UpdateSendStatus(id, isSend);
        }


        /// <summary>
        /// 获取所有的中奖记录
        /// </summary>
        [NoLogin]
        public object GetLuckRecoredList(int pageIndex = 1, int pageSize = 100)
        {
            var list = LuckBigWheel.GetLuckRecordList();
            foreach (var luckRecord in list)
            {
                if (string.IsNullOrEmpty(luckRecord.UserName))
                {
                    luckRecord.UserName = "匿名";
                }
                else if (luckRecord.UserName.Length == 1)
                {
                    luckRecord.UserName = luckRecord.UserName + "**";
                }
                else
                {
                    luckRecord.UserName = luckRecord.UserName[0] + "**";
                }
                if (string.IsNullOrWhiteSpace(luckRecord.F_CompanyName))
                {
                    luckRecord.F_CompanyName = "匿名公司";
                }
                if (string.IsNullOrWhiteSpace(luckRecord.F_ProductName))
                {
                    luckRecord.F_ProductName = "大转盘奖品";
                }
                if (luckRecord.PhoneNumber.Length == 11)
                {
                    var phone = luckRecord.PhoneNumber;
                    luckRecord.PhoneNumber = luckRecord.PhoneNumber.Substring(0, 3) + "****";
                    luckRecord.PhoneNumber += phone.Substring(7, 4);
                }
            }
            
            return list;
        }

        [NoLogin]
        public object GetLuckRecordCount()
        {
            return LuckBigWheel.GetLuckRecordCount();
        }

        /// <summary>
        /// 获取中奖人数
        /// </summary>
        [Login]
        public async Task<object> GetLuckCount()
        {
            return await LuckTurntableManager_Plus.GetLuckCount();
        }

        [Login]
        public async Task<object> Get(int exchangeKid)
        {
            return await LuckTurntableManager_Plus.GetByExchangeKid(exchangeKid);
        }

        /// <summary>
        /// 中奖纪录导出
        /// </summary>
        /// <param name="cityKid">城市ID</param>
        /// <param name="brokerPhone">经纪人电话</param>
        /// <param name="brokerName">经纪人姓名</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="isSend">是否发送</param>
        /// <param name="isWin">是否中奖</param>
        /// <param name="productName">商品名称</param>
        /// <returns></returns>
        [Login]
        public async Task<object> Export(int cityKid, string brokerPhone = "", string brokerName = "", string startTime = "", string endTime = "", int isSend = -1, int isWin = -1, string productName = null)
        {
            return await LuckTurntableManager_Plus.Export(cityKid, User.CrmUserID, brokerPhone, brokerName, startTime, endTime, isSend, isWin, productName);
        }

        /// <summary>
        /// 获取参与抽奖商品列表
        /// </summary>
        /// <returns></returns>
        [Login]
        public async Task<object> GetLuckProductList()
        {
           return await GetLuckProductList();
        }

        public object LuckActivitySetting()
        {
            string beginTime =Get("beginTime");
            string endTime = Get("endTime");
            string exchangeBeginTime = Get("exchangeBeginTime", "");
            string exchangeEndTime = Get("exchangeEndTime", "");
            int isOpen = Get("isOpen", 0);
            int turnTime = Get("turnTime", 2);
            int turnPoint = Get("turnPoint", 50);
            DateTime stime_d = Convert.ToDateTime(beginTime);
            DateTime etime_d = Convert.ToDateTime(endTime).AddDays(1);

            DateTime stime = Convert.ToDateTime(beginTime + " " + exchangeBeginTime);
            DateTime etime = Convert.ToDateTime(beginTime + " " + exchangeEndTime);
            if (stime>=etime||stime_d>=etime_d)
            {
                throw new WebApiException("开始时间不允许大于结束时间");
            }
            SystemManager.AddLog(User.CrmUserID, "大转盘活动设置", "用户名：" +User.Name).Wait();
            //return new WebApiException("活动进行中，禁止修改！！！");
            return LuckActivityManager.ActivitySetting(isOpen, beginTime, endTime, exchangeBeginTime, exchangeEndTime,turnTime,turnPoint);
        }

        public object LuckActivityGet()
        {
            return LuckActivityManager.ActivityGet();
        }
    }
}
